package com.play.ballen.synthetic.ui.fragment

import android.os.Bundle
import android.widget.Button
import androidx.recyclerview.widget.LinearLayoutManager
import com.hjq.toast.ToastUtils
import com.play.ballen.R
import com.play.ballen.databinding.FragmentMallRecordBinding
import com.play.ballen.synthetic.adapter.SyntheticNewRecordAdapter
import com.play.ballen.synthetic.vm.SyntheticVM
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.yuven.baselib.component.BaseMVVMFragment
import com.yuven.baselib.utils.ResourceUtil
import com.yuven.baselib.widget.MultiStateView

class GuessListFragment : BaseMVVMFragment<SyntheticVM, FragmentMallRecordBinding>() {

    private var isVisibleToUser = false    //是否可见
    private var isPrepared = false //布局是否准备好

    private val adapter: SyntheticNewRecordAdapter by lazy {
        SyntheticNewRecordAdapter(arrayListOf())
    }

    private var type = ""

    companion object {
        val TAG: String = GuessListFragment::class.java.simpleName

        fun newInstance(type: String): GuessListFragment {
            val args = Bundle().apply {
                putString("type", type)
            }
            val fragment = GuessListFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            type = it.getString("type", "")
        }
    }

    override fun initIntent() {}

    override fun layoutId(): Int {
        return R.layout.fragment_mall_record
    }

    override fun viewModelClass(): Class<SyntheticVM> {
        return SyntheticVM::class.java
    }

    override fun initObserver() {
        super.initObserver()
        viewModel.actionGetBallFailure.observe(this) {
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()
            if (viewModel.page == 1) {
                dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_ERROR
            } else {
                ToastUtils.show(ResourceUtil.getString(R.string.load_fail))
            }
        }

        viewModel.guessLog.observe(this) {
            dataBinding.refreshLayout.finishRefresh()
            dataBinding.refreshLayout.finishLoadMore()
            if (!it.isNullOrEmpty()) {
                if (viewModel.page == 1) {
                    adapter.setList(it)
                    dataBinding.rv.scrollToPosition(0)
                    dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_CONTENT
                } else {
                    adapter.addData(it)
                }
                viewModel.page++
            } else {
                //no data
                if (viewModel.page == 1) {
                    dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_EMPTY
                } else {
                    ToastUtils.show(ResourceUtil.getString(R.string.load_end))
                }
            }
        }
    }

    override fun initView() {
        isPrepared = true

        dataBinding.msvLayout.findViewById<Button>(R.id.btnTryAgain).setOnClickListener {
            dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
            viewModel.page = 1
            viewModel.getGuessLog()
        }
    }

    override fun initData() {
        dataBinding.refreshLayout.setEnableLoadMore(true)
        dataBinding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.page = 1
                viewModel.getGuessLog()

            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.getGuessLog()
            }
        })

        dataBinding.rv.layoutManager = LinearLayoutManager(mContext)
        dataBinding.rv.adapter = adapter

        loadData()
    }

    private fun loadData() {
        if (isVisibleToUser && isPrepared) {
            dataBinding.msvLayout.viewState = MultiStateView.VIEW_STATE_LOADING
            viewModel.page = 1
            viewModel.getGuessLog()
        }
    }

    override fun setUserVisibleHint(isVisibleToUser: Boolean) {
        super.setUserVisibleHint(isVisibleToUser)
        this.isVisibleToUser = isVisibleToUser
        if (isVisibleToUser) {
            loadData()
        } else {
            isPrepared = false
        }
    }

}